import sys
from xml.dom import minidom

from todo_list import TodoList
from todo_list_item import TodoListItem

class TodoListsHandler():
	def __init__(self, filePath):
		self.filePath = filePath
		self.xmlDoc = minidom.parse(filePath)
		self.todoLists = []
		self.listDom = self.xmlDoc.getElementsByTagName('todo_list')
		
		#i = 0
		for node in self.listDom:
			newTodoList = TodoList()
			newTodoList.domNode = node
			if node.nodeType == node.ELEMENT_NODE:
				#j = 0
				for node2 in node.childNodes:
					if node2.nodeType == node.ELEMENT_NODE:
						if node2.tagName == 'title':
							newTodoList.title = node2.childNodes[0].data.strip()
						elif node2.tagName == 'id':
							newTodoList.id = node2.childNodes[0].data.strip()
						elif node2.tagName == 'closed':
							newTodoList.closed = node2.childNodes[0].data.strip()
						elif node2.tagName == 'items':
							for node3 in node2.childNodes:
								if node3.nodeType == node.ELEMENT_NODE:
									newItem = TodoListItem()
									for node4 in node3.childNodes:
										if node4.nodeType == node.ELEMENT_NODE:
											#print node4.tagName
											#print node4.childNodes[0].data
											if node4.tagName == 'title':
												newItem.title = node4.childNodes[0].data.strip()
											elif node4.tagName == 'id':
												newItem.id = node4.childNodes[0].data.strip()
											elif node4.tagName == 'description':
												newItem.description = node4.childNodes[0].data.strip()
											elif node4.tagName == 'type':
												newItem.type = node4.childNodes[0].data.strip()
									newTodoList.items.append(newItem)
										
					#j = j + 1
			self.todoLists.append(newTodoList)
			#i = i + 1
		
#		for child in self.innerTodoListsDom:
#			newTodoList = TodoList()
#			newTodoList.id = child.childNodes[0]
#			newTodoList.title = child.childNodes[1]
#			newTodoList.closed = child.childNodes[2]
#			
#			for item in child.childNodes[3]:
#				newTodoListItem = TodoListItem()
#				newTodoListItem.id = item.childNodes[0]
#				newTodoListItem.title = item.childNodes[1]
#				newTodoListItem.description = item.childNodes[2]
#				newTodoListItem.type = item.childNodes[3]
#				
#				newTodoList.items.append(newTodoListItem)
#				
#			self.todoLists.append(newTodoList)
		
	def getTodoLists(self):
		return self.todoLists
		
	def writeTodoLists(self):
		doc = minidom.Document()
		main = doc.createElement("todo_lists")
		doc.appendChild(main)
		
		# Create the <wml> base element
		for todoList in self.todoLists:
			todoListElem = doc.createElement("todo_list")
			
			# genero los campos de texto
			todoListId = doc.createElement("id")
			id = doc.createTextNode(todoList.id)
			todoListId.appendChild(id)
			
			todoListTitle = doc.createElement("title")
			title = doc.createTextNode(todoList.title)
			todoListTitle.appendChild(title)
			
			todoListClosed = doc.createElement("closed")
			closed = doc.createTextNode(todoList.closed)
			todoListClosed.appendChild(closed)
			
			todoListElem.appendChild(todoListId)
			todoListElem.appendChild(todoListTitle)
			todoListElem.appendChild(todoListClosed)
			
			# genero los items
			items = doc.createElement("items")
			
			for item in todoList.items:
				itemElem = doc.createElement("item")
				
				# genero los campos de texto
				itemId = doc.createElement("id")
				id = doc.createTextNode(item.id)
				itemId.appendChild(id)
				
				itemTitle = doc.createElement("title")
				title = doc.createTextNode(item.title)
				itemTitle.appendChild(title)
				
				itemDescription = doc.createElement("description")
				description = doc.createTextNode(item.description)
				itemDescription.appendChild(description)
				
				itemType = doc.createElement("type")
				typeNode = doc.createTextNode(item.type)
				itemType.appendChild(typeNode)
			
				itemElem.appendChild(itemId)
				itemElem.appendChild(itemTitle)
				itemElem.appendChild(itemDescription)
				itemElem.appendChild(itemType)
				items.appendChild(itemElem)
			
			todoListElem.appendChild(items)
			main.appendChild(todoListElem)

		fp = open(self.filePath,"w")
		# writexml(self, writer, indent='', addindent='', newl='', encoding=None)
		doc.writexml(fp, "    ", "", "\n", "UTF-8")

	

def read_todo_lists():
	"""docstring for read_todo_lists"""
	#parse("./resources/example_database.xml", TodoListsHandler(todoLists))
	handler = TodoListsHandler('../../resources/example_database.xml')
	todoLists = handler.getTodoLists()
	
	for todoList in todoLists:
		print todoList.brief()
		
	print "y ahoraaaaa"
	handler.addNewElementAtEnd()

if __name__ == '__main__':
	read_todo_lists()